<?php

/**
 * 京东认证Token 模型
 *
 * @author cjj
 */
define("TOK_OUT_TIME",3600);
include_once APPPATH . "core/base_model.php";
class authorizetokenmodel extends base_model {

    private static $table = 'authorize_token';

    const B2C_USER_ID = 8899;    
    
    public function __construct() {
        parent::__construct();
        $this->load->database('apach');
    }

        /**
     * 获取所有有效auth
     *
     * @return array
     */
    public function getSellersByChannel($channel_id) {
        $this->db->where('channel_id',$channel_id);
        $this->db->where('user_id !=',0);
        $this->db->where('huaji_user_id !=',0);
        $this->db->order_by('id','ASC');
        $result = $this->get(self::$table);
        if ($result)
            return $result->result_array();
        else
            return array();
    }

    public function getEffectiveSellers($channel_id) {
        $this->db->where('channel_id',$channel_id);
        $this->db->where('user_id !=',0);
        $this->db->where('huaji_user_id !=',0);
        $this->db->where('expire_time >',time());
        $this->db->where('token !=',"");
        $this->db->order_by('id','ASC');
        $result = $this->get(self::$table);
        if ($result)
            return $result->result_array();
        else
            return array();
    }
    
    /**
     * 根据淘宝tid列表读取派单信息
     *
     * @param $jd_uid
     *
     * @return mixed
     */
    public function getToken($user_id,$channel_id=1) {
        if (!is_numeric($user_id)) {
            return NULL;
        }
        $this->db->where('user_id', $user_id);
        $this->db->where('channel_id', $channel_id);
        $this->db->limit(1);
        return $this->get(self::$table)->row();
    }
    
    public function edit($user_id, $data = array()) {
        if (!$user_id || !$data) {
            return null;
        }
        $this->db->where('user_id', $user_id);
        $result = $this->update(self::$table, $data);
        return $result;
    }
}

?>
